<?php
	require_once("user_session.php");
	$olderPId = $_POST['olderPId'];
	$dId = $_POST['dId'];
	
	# Re-Build the paragraph data 
	
	# we use:
	# $paragraphs: array with $paragraph
	# $paragraph : array with three element
	# $p_content : the first element in $paragraph
	# $p_ref : array, the second element in $paragraph
	# $p_internal_link  : array the third element in $paragraph
	$p_ref = array();
	$p_internal_link = array();
	
	$ider = 0;
	
	$postArrayKeys = array_keys($_POST);
	
	#print_r($_POST);
	#echo "<br />======================================<br />";
	#print_r($postArrayKeys);
	#echo "<br />======================================<br />";

	$i = 0;//paragraph counter
	$j = 0;//one paragraph item counter
	for($k = 0;$k<count($postArrayKeys);$k ++){
		$tempJ = $j;
		#echo $postArrayKeys[$k] . " p_".$i ."<br />";
		if(ereg("p_".$i, $postArrayKeys[$k])){
		# the case of reading a specific paragraph of p_$i
			#echo "====>" . $i . " " . $j ."<br />";
			if(ereg("internal", $postArrayKeys[$k])){
				$p_internal_link = $_POST[$postArrayKeys[$k]];
			} else if (ereg("ref", $postArrayKeys[$k])){
				$p_ref = $_POST[$postArrayKeys[$k]];
			} else if (ereg("content", $postArrayKeys[$k])){
				$p_content = $_POST[$postArrayKeys[$k]];
			}
			$j ++;
		}  else if($j==0 && $i<count($postArrayKeys)){
		# the case of finish reading an empty p_$i
		# so we need to:
		#  1) begin to read p_($i+1):  $i++ 
		#  2) read this key again:     $k--
			$i++;
			$k--;
		} else if($j!=0 && $tempJ == $j){
		# the case of finish reading p_$i and have read one key of p_($i+1)
		# so we need to:
		#  1) begin to read p_($i+1):  $i++ 
		#  2) read this key again:     $k--
		#  3) reset paragraph items:   $j = 0
		#  4) handle the data we read
			
			$paragraph[0] = $p_content;
			$paragraph[1] = $p_ref;
			$paragraph[2] = $p_internal_link;
			$paragraphs[] = $paragraph;
			
			if(count($p_internal_link)!=0){
				foreach($p_internal_link as $internalLinkId){
					if(EKDocument::testExist($internalLinkId) == false){
						$ider ++;
						echo "<script language=javascript>\n".
					        "alert(\"Internal Link: ". $internalLinkId ." is not legal\");\n".
					        "</script>";
					}
				}
			}
			
			unset($paragraph);
			$i ++;
			$k = $k - 1;
			$j = 0;
		}
		
	}
	
	#print_r($paragraphs);
	#echo "<br />======================================<br />";

	if($ider == 0){
		
		$pr = EKParagraph::getElementValueById($olderPId, "EKParagraph", "prId");
		$pa = EKParagraphRelationship::getParagraphIdsById($pr);
		foreach($pa as $paragraphId){
			if($_SESSION['userid'] == EKElement::getElementValueById($paragraphId, "EKParagraph", "authorId"))
			{
				$olderPId = $paragraphId;
			}
		}
		
		# Save Doc
		$id = EKParagraph::updateParagraphNode($olderPId, $paragraphs[0]);
		
		echo "<script language=javascript>\n".
					        "alert(\"". "Paragraph modified successfully! " ."\");\n".
					        "self.location=\"document.php?id=".$dId."&method=v\";\n".
					        "</script>";
	} else {
		echo "<script language=javascript>window.history.back();</script>";
	}

?>